var status = 0;
var numberOrShopName = '';
$(function() {

	//跳转登录页
	var userId = sessionStorage.getItem("userId");
	if(userId == null) {
		location.href = '1.0.0denglu.html';
	} else {
		loadMyOrderCount(); //查询订单数量
		var status1 = getvl("status");
		if(status1 != null) {
			loadMyEssay(status1, numberOrShopName);
		} else {
			loadMyEssay(status, numberOrShopName);
		}

	}

	
});

function loadMyOrderCount() {
	$("#orderStatus").html("");
	$('#orderCenter').html("");
	$.ajax({
		url: urlcore + "/api/order/selectOrderCount",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {

			if(data.success == true) {

				var da = data.data;

				var thislist =
					'<ul class="wode_title fl">' +
					'<li class="curr">' +
					'<a href="javascript:selectOrderByStatus(0);">全部订单(' + da.count + ')</a>' +
					'</li>' +
					'<li>|</li>' +
					'<li>' +
					'<a href="javascript:selectOrderByStatus(1);">待付款(' + da.DZFCount + ')</a>' +
					'</li>' +
					'<li>|</li>' +
					'<li>' +
					'<a href="javascript:selectOrderByStatus(2);">待发货(' + da.DFHCount + ')</a>' +
					'</li>' +
					'<li>|</li>' +
					'<li>' +
					'<a href="javascript:selectOrderByStatus(3);">待收货(' + da.DSHCount + ')</a>' +
					'</li>' +
					'<li>|</li>' +
					'<li>' +
					'<a href="javascript:selectOrderByStatus(4);">待评价(' + da.DPJCount + ')</a>' +
					'</li>' +
					'</ul>';
				$('#orderStatus').append(thislist);
				$('.wode_title li').on('click', function() {
					$(this).addClass('curr').addClass('a').siblings().removeClass('curr');
				});
				
				
				var orderCenter=
				'<div class="public_left">'+
				'<dl class="wode01">'+
						'<dt>订单中心</dt>'+
						'<dd>'+
							'<a class="curr" href="javascript:selectOrderByStatus(0);">全部订单</a>'+
						'</dd>'+
						'<dd>'+
							'<a href="javascript:selectOrderByStatus(1);">待付款</a>'+
						'</dd>'+
						'<dd>'+
							'<a href="javascript:selectOrderByStatus(2);">待发货</a>'+
						'</dd>'+
						'<dd>'+
							'<a href="javascript:selectOrderByStatus(3);">待收货</a>'+
						'</dd>'+
						'<dd>'+
							'<a href="javascript:selectOrderByStatus(4);">待评价</a>'+
						'</dd>'+
					'</dl>'+
					'<dl class="wode02">'+
						'<dt>我的收藏</dt>'+
						'<dd>'+
							'<a href="21.0.0wode_shoucang_goods.html">收藏的商品</a>'+
						'</dd>'+
						'<dd>'+
							'<a href="22.0.0wode_shoucang_dianpu.html">收藏的店铺</a>'+
						'</dd>'+
					'</dl>'+
					'<dl class="wode03">'+
						'<dt>基础设置</dt>'+
						'<dd>'+
							'<a href="24.0.0wode_dizhi.html">收货地址管理</a>'+
						'</dd>'+
					'</dl>'+
					'</div>';
					
					
					$('#orderCenter').append(orderCenter);
						$('.public_left dl dd a').on('click', function() {
							$(this).parents('dl').find('dd').find('a').removeClass('curr');
							$(this).addClass('curr');
							
		                  //$(this).addClass('curr').addClass('a').siblings().removeClass('curr');
	                });
				
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}

var currentPage = 1;

//列表
//loadMyEssay(status, numberOrShopName);

function loadMyEssay(status, numberOrShopName) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#diandanList").html("");
		$.ajax({
			url: urlcore + "/api/order/selectOrderForGoodsList1?status=" + status + "&numberOrShopName=" + numberOrShopName + "&size=" + 5 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			async: false,
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {
						var goodslist = '';
						//var money = 0;
						if(n.goodsCarList!=null){
								$.each(n.goodsCarList, function(i, m) {
								var saleNum = 0;
								$.each(m.myStockList, function(i, g) {
									saleNum += g.saleNum;
								})
								//var money = m.tradePrice * m.goodsNumber;
	
								var shixiao = '';
								if(m.goods.status == 2 || m.goods.status == 3) {
									shixiao = '(此商品已失效，请重新选购)';
								}
								var coverUrl = isHasImg(m.goods.coverUrl);
								
								var goodsTitle = m.goods.goodsTitle == "" ? "" : m.goods.goodsTitle;
								var tradePrice = m.goods.tradePrice == "" ? "" : m.goods.tradePrice;
								var packPrice = m.goods.packPrice == "" ? "" : m.goods.packPrice;
	
								var packRate = m.packRate;
								/*if(m.goodsType == 1) {
									packRate = m.packRate;
								} else {
									packRate = m.packRate;
								}*/
								goodslist +=
									'<tr>' +
									'<td width="478">' +
									'<dl class="clearfix">' +
									'<dt>' +
									'<img src="' + coverUrl + '">' +
									'</dt>' +
									'<dd>' +
									'<h4>' + goodsTitle + '' + shixiao + '</h4>' +
									'<h6>&yen;' + packRate + '</h6>' +
									'</dd>' +
									'</dl>' +
									'</td>' +
									'<td>' +
									'<div>x' + saleNum + '</div>' +
									'</td>' +
									'</tr>';
	
							});
						}
						
						var fk = '';
						if(n.status == 0) {
							fk = '<td>' +
								'<em class="red fukuan1">立即付款</em>' +
								'<span onclick="cancelDinDan(' + n.id + ')">取消订单</span>' +
								'</td>';
						}

						var fkstatus = '';
						if(n.status == 0) {
							fkstatus = '<span>等待买家付款</span>';
						} else if(n.status == 1 && n.tradeStatus == 0) {
							fkstatus = '<span>等待发货</span>';

						} else if(n.status == 1 && n.tradeStatus == 1) {
							fkstatus = '<span>已发货</span>';

						} else if(n.status == 2 && n.tradeStatus == 2) {

							fkstatus = '<span>确认收货</span>';
						}

						var thislist =
							'<div class="putong_list_title">' +
							'<span>' + new Date(n.gmtDatetime).pattern("yyyy-MM-dd") + '</span>' +
							'<span>订单号 : ' + n.orderNumber + '</span>' +
							'<a href="10.1.0dianpu_detail.html?id=' + n.shop.id + '"><em>' + n.shop.storeName + '</em></a>' +
							'</div>' +
							'<div class="putong_list_con clearfix">' +
							'<table>' +
							'<tr>' +
							'<td width="568" class="pitong_list_left">' +
							'<table class="table_b">' +
							goodslist +
							'</table>' +
							'</td>' +
							'<td width="330" class="pitong_list_right">' +
							'<table>' +
							'<tr class="money1">' +
							'<td width="100" class="h100 money2">' +
							'<div class="money3"><input type="hidden" id="id" class="id" value="' + n.id + '"/><input type="hidden" id="dindanmoney" class="dindanmoney" value="' + n.payMoney + '"/>&yen;' + n.payMoney + '</div>' +
							'</td>' +
							'<td width="110">' +
							fkstatus +
							'<a href="20.1.0daifukuan_detail.html?id=' + n.id + '">订单详情</a>' +
							'</td>' +
							fk +
							'</tr>' +
							'</table>' +
							'</td>' +
							'</tr>' +
							'</table>' +
							'</div>';
						$('#diandanList').append(thislist);
						$(function() {

							$('.fukuan1').click(function() {
								//支付方式
								/*var payType = $('input:radio[name="bank"]:checked').length;
								if(payType == 0) {
									alert("请选择支付方式");
									return;
								}*/
								
								var id = $(this).parents('.money1').find('td[class*=money2]').find('div[class*=money3]').find('input[class*=id]').val();
								var money = $(this).parents('.money1').find('td[class*=money2]').find('div[class*=money3]').find('input[class*=dindanmoney]').val();
								if(money != '') {
									//把支付金额存入
									sessionStorage.setItem('orderId', id);
									sessionStorage.setItem('money', money);
									window.location.href = "choice_pay.html";
								}
								//	$(".public_simi_bg1").fadeOut();
							})
						})

					});

					/*$(function() {
						$('.fukuan1').click(function() {
							$(".public_simi_bg1").fadeIn();

							$(".public_shuliang h5 span,.public_shuliang_footer a").on('click', function() {
								$(".public_simi_bg1").fadeOut();
							})
						})

						
					})*/
					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					layer.alert("未登录！");
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}

function selectOrderByStatus(status) {
	loadMyEssay(status, '');
}

function selectOrderByShop() {
	var numberOrShopName = $("#keyBoard").val().trim();
	loadMyEssay('', numberOrShopName);
}

function cancelDinDan(id) {
	if(!layer.confirm("确认取消吗?")) {
		return;
	}
	$.ajax({
		url: urlcore + '/api/order/update1',
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"id": id,
			"status": 7
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
				layer.alert("取消成功");
				//跳转列表页
				window.location.href = "20.0.0wode_dingdan.html";
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href = "1.0.0denglu.html";
				}
			} else {
				layer.alert(data.msg);
			}

		},
		error: function() {
			layer.alert("error");
		}

	});
}